//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.lyyzoo.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.Validate;

public abstract class Cryptos {
    public static final String SHA1 = "SHA-1";
    public static final String MD5 = "MD5";
    private static SecureRandom random = new SecureRandom();

    public Cryptos() {
    }

    public static String encryptBase64(String key) throws Exception {
        return Base64.encodeBase64String(key.getBytes());
    }

    public static String encryptBase64(byte[] key) throws Exception {
        return Base64.encodeBase64String(key);
    }

    public static String decryptBase64(String key) throws Exception {
        return new String(Base64.decodeBase64(key), Charset.forName("UTF-8"));
    }

    public static byte[] decryptBase64ToByte(String key) {
        return Base64.decodeBase64(key);
    }

    public static String encryptMd5(String input) {
        byte[] encrypted = digest(input.getBytes(), "MD5", (byte[])null, 1);
        return Encodes.encodeHex(encrypted);
    }

    public static String encryptMd5(String input, String salt) {
        return Md5Crypt.md5Crypt(input.getBytes(), salt);
    }

    public static String encryptMd5(String data, String salt, String prefix) {
        return Md5Crypt.md5Crypt(data.getBytes(), salt, prefix);
    }

    public static byte[] encryptMd5(InputStream input) {
        return digest(input, "MD5");
    }

    public static String encryptMd5Apr(String data, String salt) {
        return Md5Crypt.apr1Crypt(data.getBytes(), salt);
    }

    public static byte[] encryptSha1(InputStream input) throws IOException {
        return digest(input, "SHA-1");
    }

    public static String encryptSha1(String input) {
        return Encodes.encodeHex(encryptSha1ToByte(input));
    }

    public static byte[] encryptSha1ToByte(String input) {
        return digest(input.getBytes(), "SHA-1", (byte[])null, 1);
    }

    public static byte[] encryptSha1(byte[] input) {
        return digest(input, "SHA-1", (byte[])null, 1);
    }

    public static byte[] encryptSha1(byte[] input, byte[] salt) {
        return digest(input, "SHA-1", salt, 1);
    }

    public static byte[] encryptSha1(byte[] input, byte[] salt, int iterations) {
        return digest(input, "SHA-1", salt, iterations);
    }

    public static String encryptSha1(String input, String salt) {
        return encryptSha1((String)input, (String)salt, 1);
    }

    public static String encryptSha1(String input, String salt, int iterations) {
        byte[] encrypted = digest(input.getBytes(), "SHA-1", salt.getBytes(), iterations);
        return Encodes.encodeHex(encrypted);
    }

    public static String encryptSha1WithHexSalt(String input, String salt, int iterations) {
        byte[] salts = Encodes.decodeHex(salt);
        byte[] encrypted = digest(input.getBytes(), "SHA-1", salts, iterations);
        return Encodes.encodeHex(encrypted);
    }

    private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
        try {
            MessageDigest digest = MessageDigest.getInstance(algorithm);
            if (salt != null) {
                digest.update(salt);
            }

            byte[] result = digest.digest(input);

            for(int i = 1; i < iterations; ++i) {
                digest.reset();
                result = digest.digest(result);
            }

            return result;
        } catch (GeneralSecurityException var7) {
            throw Exceptions.unchecked(var7);
        }
    }

    private static byte[] digest(InputStream input, String algorithm) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            int bufferLength = 8192;
            byte[] buffer = new byte[bufferLength];

            for(int read = input.read(buffer, 0, bufferLength); read > -1; read = input.read(buffer, 0, bufferLength)) {
                messageDigest.update(buffer, 0, read);
            }

            return messageDigest.digest();
        } catch (Exception var6) {
            throw Exceptions.unchecked(var6);
        }
    }

    public static byte[] generateSalt(int numBytes) {
        Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", (long)numBytes);
        byte[] bytes = new byte[numBytes];
        random.nextBytes(bytes);
        return bytes;
    }
}
